import Mock from 'mockjs'

const dataList = Mock.mock({
  "dataList|400": [{
    yybName: '@cname',      // 注意字段名是yybName
    "khh": '@string("number", 10)',
    khxm: '@cname',
    indexSource: '@pick(["BJ","SZ","SH", "NJZQ"])',
    indexName: '@name',
    'alertLevel|1-5': 1,
    groupName: '@name',
    createTime: '@datetime', // 注意字段名是createTime
    state: '1',
  }]
})

Mock.mock('/mock/alert/getList', 'post', (req) => {
  const params = JSON.parse(req.body)
  
  const {
    khxm,
    indexSource,
    yyb,        // 对应数据中的yybName
    indexName,
    alertLevel,
    khh,
    pageNum = 1, // 添加默认值
    pageSize = 10,
    startTime,   // 时间范围起点
    endTime      // 时间范围终点
  } = params

  // 步骤1：过滤数据（只在参数存在时应用过滤）
  let filteredData = dataList.dataList.filter(item => {
    // 营业部过滤（注意字段名对应）
    if (yyb && item.yybName !== yyb) return false
    
    // 客户姓名过滤
    if (khxm && item.khxm !== khxm) return false
    
    // 指标来源过滤
    if (indexSource && item.indexSource !== indexSource) return false
    
    // 指标名称过滤
    if (indexName && item.indexName !== indexName) return false
    
    // 预警级别过滤
    if (alertLevel && item.alertLevel != alertLevel) return false // 使用宽松相等
    
    // 客户号过滤
    if (khh && item.khh !== khh) return false
    
    // 时间范围过滤（核心修正）
    if (startTime && endTime) {
      const createTime = new Date(item.createTime).getTime()
      const start = new Date(startTime).getTime()
      const end = new Date(endTime).getTime()
      
      if (createTime < start || createTime > end) return false
    }
    
    return true
  })

  // 步骤2：计算分页数据
  const total = filteredData.length
  const startIndex = (pageNum - 1) * pageSize
  const pageData = filteredData.slice(startIndex, startIndex + pageSize)

  // 步骤3：返回结果
  return {
    result_code: 0,
    data: {
      list: pageData,
      total: total  // 返回过滤后的总数
    },
    result_msg: ''
  }
})
const noRepeatList = [{
  yybName: '1',
  khh: '1',
  khxm: '1',
  jys: '1',
  indexSource: '1',
  indexName: '1',
  alertLevel: '1',
  groupName: '11',
  createTime: '1',
  state: '1',
  alertId: 1,
}]
Mock.mock('/mock/alert/getNoRepeatList', 'post', () => {
  return {
    result_code: 0,
    data: {
      list: noRepeatList,
      total: noRepeatList.length
    }
  }
})

Mock.mock('/mock/warningConfig/getCfgIndexName', 'get', () => {
  return {
    result_code: 0,
    data: {
      list: dataList,
      total: dataList.length
    }
  }
})
const customerAlertDetail = [{
  alertLevel: '1',
  alertCondition: '1',
  alertValue: '1',
  alertThreshold: '1',
  warningFlowList: '0',
}]
Mock.mock('/mock/alert/selectCustomerAlertDetail', 'post', () => {
  return {
    result_code: 0,
    data: {
      list: customerAlertDetail,
      total: customerAlertDetail.length
    }
  }
})